from abc import ABC, abstractmethod
from shared.crypto import AESCipher
import zstandard as zstd

class BaseBackup(ABC):
    def __init__(self, graph_client, storage):
        self.client = graph_client
        self.storage = storage
        self.compressor = zstd.ZstdCompressor()
        self.cipher = AESCipher()
    
    @abstractmethod
    def execute_backup(self, target: str):
        """执行备份操作"""
        pass
    
    def _process_data(self, data: bytes) -> bytes:
        """通用数据处理流水线"""
        compressed = self.compressor.compress(data)
        return self.cipher.encrypt(compressed)
    
    def _save_checkpoint(self, checkpoint_data: dict):
        """保存检查点"""
        self.storage.store_metadata(
            "checkpoints.json",
            json.dumps(checkpoint_data).encode()
        )
    
    def _handle_rate_limit(self, response):
        """处理速率限制"""
        if response.status_code == 429:
            retry_after = int(response.headers.get("Retry-After", 60))
            time.sleep(retry_after)
            return True
        return False